package com.msmall.base.rms.controller;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.msmall.base.PaginationSupport;
import com.msmall.base.PaginationSupportUtil;
import com.msmall.base.ReturnBase;
import com.msmall.base.rms.manager.RMSManager;
import com.msmall.base.rms.po.Module;
import com.msmall.base.rms.util.RMSUtils;
import com.msmall.erp.base.Constant;
import com.msmall.erp.base.LoginConstant;
import com.msmall.erp.sso.vo.AdministerVO;

@Controller
public class ModuleController {

	private static final Logger log = Logger.getLogger(ModuleController.class);
	
	@Autowired
	private RMSManager rmsManager;

	@RequestMapping("/rms/module/getModuleTree")
	public void getModuleTree(HttpServletRequest request, HttpServletResponse response) {
		try{
			HttpSession session = request.getSession();
			// 从session 里面获取用户名的信息
			AdministerVO vo = (AdministerVO) session.getAttribute(LoginConstant.HTTP_SESSION_KEY_ADMINISTER);
			response.setContentType("text/plain;charset=UTF-8");
			PrintWriter out = response.getWriter();
			JSONObject json = rmsManager.getModuleByAdministerJson(vo.getId());
			if(json != null){
				out.print(json.toString());
			}else{
				log.info(" =====> NULL ");
				out.print("");
			}
		}catch(Exception e){
			log.error(e);
		}
	}
	
	@RequestMapping("/rms/module/index")
	public String index(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) {
		try{
			return "rms.module.index";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	@RequestMapping("/rms/module/search")
	public String search(HttpServletRequest request, HttpServletResponse response, @RequestParam String moduleName, Map<String, Object> model) {
		try{
			PaginationSupport page = PaginationSupportUtil.get(request);
			model.put("page", page);
			rmsManager.searchModule(moduleName, page);
			return "rms.module.search.result";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	/**
	 * 进入创建/更新实体页面
	 */
	@RequestMapping("/rms/module/entity")
	public String entity(HttpServletRequest request, HttpServletResponse response, @RequestParam Integer moduleid, Map<String, Object> model) {
		try{
			if(moduleid != null && moduleid != 0){
				Module module = rmsManager.getModule(moduleid);
				model.put("entity", module);
			}
			
			return "rms.module.entity";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	/**
	 * 保存创建/更新的实体
	 */
	@RequestMapping("/rms/module/save")
	public String save(HttpServletRequest request, HttpServletResponse response, Module module, Map<String, Object> model) {
		try{
			rmsManager.saveModule(module);
			return "rms.module.index";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	
	/**
	 * AJAX 获得模块的子模块/父模块列表
	 * @param moduleid
	 * @return
	 */
	@RequestMapping("/rms/module/cascade")
	public void cascadeModule(HttpServletRequest request, HttpServletResponse response,@RequestParam Integer moduleid, @RequestParam int type){
		try{
			response.setContentType("text/plain;charset=UTF-8");
			PrintWriter out = response.getWriter();
			List<Module> modules = null;
			if(moduleid == null || moduleid == 0){
				modules = rmsManager.getTopModule();
			}else {
				if(type == -1){
					modules = rmsManager.getParentModule(moduleid);
				}
				if(type == 1){
					modules = rmsManager.getChildrenModule(moduleid);
				}
			}
			if(modules != null){
				JSONObject json = new JSONObject();
				json.put("items", modules);
				out.print(json.toString());
			}else{
				log.info(" =====> NULL ");
				out.print("");
			}
		}catch(Exception e){
			log.error(e);
		}
	}
	/**
	 * 删除模块
	 * @param moduleid
	 * @return
	 */
	@RequestMapping("/rms/module/remove")
	public void remove(HttpServletRequest request, HttpServletResponse response,@RequestParam Integer moduleid){
		response.setContentType("text/plain;charset=UTF-8");
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			out = response.getWriter();
			rmsManager.removeModule(moduleid);
			json.put( ReturnBase.RETURN_CODE_KEY, ReturnBase.RETURN_CODE_SUCCESS);
		}catch(Exception e){
			log.error(e);
			json.put(ReturnBase.RETURN_CODE_KEY, ReturnBase.RETURN_CODE_FAILED);
		}
		out.print(json.toString());
	}
}
